package com.example.service;

import com.example.dao.ProductInfoDao;
import com.example.entity.ProductInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class ProductInfoService {
    @Resource
    private ProductInfoDao productInfoDao;

    public void insert(ProductInfo productInfo) {
        productInfo.setTime(new Date());
        productInfoDao.insert(productInfo);
    }

    public void update(ProductInfo productInfo) {
        productInfoDao.updateByPrimaryKey(productInfo);
    }

    public void remove(Long id) {
        productInfoDao.deleteByPrimaryKey(id);
    }

    public PageInfo<ProductInfo> getProductInfoList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductInfo> infos = productInfoDao.selectAll();
        return PageInfo.of(infos);
    }

    public PageInfo<ProductInfo> getProductInfoListByCondition(Integer pageNum, Integer pageSize, ProductInfo productInfo) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(ProductInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(productInfo);
        List<ProductInfo> infos = productInfoDao.selectByExample(example);
        return PageInfo.of(infos);
    }
}
